package com.zuihai.app.mvp.ui.widget.behavior;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.widget.behavior.helper.HeaderScrollingViewBehavior;

import java.util.List;

/**
 * Created by cqll on 2016/12/9.
 */

public class MyPageListBehavior extends HeaderScrollingViewBehavior {
    private int mMaxScrollRange;

    public MyPageListBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        if (isDependOn(dependency)) {
            LogUtils.e(dependency.getContext().getResources().getDimensionPixelSize(R.dimen.dp_forty_two));
            LogUtils.e(dependency.getHeight());
            mMaxScrollRange = dependency.getHeight() - dependency.getContext().getResources().getDimensionPixelSize(R.dimen.dp_forty_two);
            return true;
        }
        return false;
    }


    @Override
    public View findFirstDependency(List<View> views) {
        for (View view : views) {
            if (isDependOn(view))
                return view;
        }
        return null;
    }

    @Override
    protected int getScrollRange(View v) {
        if (isDependOn(v)) {
            return mMaxScrollRange;
        } else {
            return super.getScrollRange(v);
        }
    }

    private boolean isDependOn(View dependency) {
        return dependency instanceof AppBarLayout;
    }

    //跟随tab移动
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        child.setTranslationY(dependency.getTranslationY());
        return true;
    }
}
